/*
 * Copyright (C) 2021 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_i2c.h"
#include "stm32_task.h"

#define STM32_DEVICE_ADDR   0x18
#define STM32_READ_ADDR     ((0x18<<1)|0x1)
#define STM32_WRITE_ADDR    ((0x18<<1)|0x0)

#define QUEUE_SIZE 3
osMessageQueueId_t qid;
rmc_data RecRMC;
rmc_data *const pRecRMC = &RecRMC; // RMC解析输出缓存区,该指针指向固定的地址
char RX_BUF[100];
/*************************************
功能：GPSRMC数据解析
作者：xinxue
日期：2023.8.31
参数：无
返回值：无
**************************************/
void AnalysisRMC(rmc_data *pRecRMC) // GPSRMC数据解析
{
	int i, n = 0;

	for (i = 0; RX_BUF[i] != '\0'; i++)
	{
		if (RX_BUF[i] == ',')
		{
			n++;
			i++; // 跳过 “，”
			// strncpy(sub, string+12,13);   将string从string[12]开始，截取13个复制到sub中
			if (n == 1) //
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->time[0] = '\0';
					i--;
				}
				else 
				{
					strncpy(pRecRMC->time, RX_BUF + i, 10);
					pRecRMC->time[10] = '\0';
					i = i + 9;
				}
				
			} // i=i+10;目的是为了减少循环次数，提高效率
			else if (n == 2)
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->state[0] = '\0';
					i--;
				}
				else 
				{
					strncpy(pRecRMC->state, RX_BUF + i, 1);
					pRecRMC->state[1] = '\0';
				}
			}
			else if (n == 3)
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->lat[0] = '\0';
					i--;
				}
				else 
				{
					strncpy(pRecRMC->lat, RX_BUF + i, 9);
					pRecRMC->lat[9] = '\0';
					i = i + 8;
				}
			}
			// else if (n == 4)
			// {
			// 	strncpy(pRecRMC->NS, RX_BUF + i, 1);
			// }
			else if (n == 4)
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->lng[0] = '\0';
					i--;
				}
				else 
				{
					strncpy(pRecRMC->lng, RX_BUF + i, 10);
					pRecRMC->lng[10] = '\0';
					i = i + 9;
				}
			}
			// else if (n == 6)
			// {
			// 	strncpy(pRecRMC->WE, RX_BUF + i, 1);
			// }
			else if (n == 5)
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->speed[0] = '\0';
					i--;
				}
				else 
				{
					int len = 0;
					while (1)
					{
						len++;
						if (RX_BUF[i + len] == ',')
							break;
					}
					strncpy(pRecRMC->speed, RX_BUF + i, len);
					pRecRMC->speed[len+1] = '\0';
					i = i + len - 1;
				}
			}
			else if (n == 6)
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->date[0] = '\0';
					i--;
				}
				else 
				{
					strncpy(pRecRMC->date, RX_BUF + i, 6);
					pRecRMC->date[6] = '\0';
					i = i + 5;
				}
			}
			else if (n == 7)
			{
				if(RX_BUF[i] == ',')
				{
					pRecRMC->Temperature[0] = '\0';
					i--;
				}
				else 
				{
					strncpy(pRecRMC->Temperature, RX_BUF + i, 6);
					pRecRMC->Temperature[6] = '\0';
					i = i + 5;
				}
			}
		}
	}

	memset(RX_BUF, 0, sizeof RX_BUF); // 清空接收缓存区
}
//初始化以及 aht20线程中5s获取一次温湿度
void STM32Task()
{
	hi_io_set_func(HI_IO_NAME_GPIO_0, HI_IO_FUNC_GPIO_0_I2C1_SDA);
    hi_io_set_func(HI_IO_NAME_GPIO_1, HI_IO_FUNC_GPIO_1_I2C1_SCL);
    int ret = hi_i2c_init(HI_I2C_IDX_1, 100 * 1000);
    printf("i2c ret:%d\n",ret);
	// hi_i2c_data send_data = { 0 };
	// char WRITE_DATA[3] = {0xff, 0x01, 0xef};
	// send_data.receive_buf = WRITE_DATA;
	// send_data.receive_len = 100;
	// hi_i2c_read(HI_I2C_IDX_1, STM32_WRITE_ADDR, &send_data);
    //rmc_data sentry;
}
//获取温湿度
rmc_data stm32_get()
{
    hi_i2c_data data = { 0 };
	data.receive_buf = RX_BUF;
	data.receive_len = 100;
	hi_i2c_read(HI_I2C_IDX_1, STM32_READ_ADDR, &data);
	// printf("stm32 buff:%s \n buff sizeof:%d\n", data.receive_buf, strlen(data.receive_buf));
	AnalysisRMC(pRecRMC);
	osMessageQueuePut(qid, (void *)pRecRMC, sizeof(rmc_data), osWaitForever);
	int count = osMessageQueueGetCount(qid);
}
//创建线程
void stm32_init(void)
{
    printf("STM32 init\n");
    qid = osMessageQueueNew(QUEUE_SIZE, sizeof(rmc_data), NULL);

	STM32Task();
}



